package org.jeecg.modules.oa.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.oa.entity.OaReportPlan;
import org.jeecg.modules.oa.mapper.OaReportPlanMapper;
import org.jeecg.modules.oa.service.IOaReportPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 周报子表
 * @Author: jeecg-boot
 * @Date: 2019-09-17
 * @Version: V1.0
 */
@Service
public class OaReportPlanServiceImpl extends ServiceImpl<OaReportPlanMapper, OaReportPlan> implements IOaReportPlanService {
    @Autowired
    private OaReportPlanMapper reportPlanMapper;

    public void savePlanWeek(String pid, Date workDateFrom, Date workDateTo, List<OaReportPlan> weekList,String planStatus) {
        List<String> ids1 = reportPlanMapper.getByParentIdAndPlanStatus(pid, planStatus);//已经存在的计划
        List<String> ids2 = new ArrayList<>();//新增或编辑的计划
        if (CollectionUtil.isNotEmpty(weekList)) {
            for (int i = 0; i < weekList.size(); i++) {
                if (StringUtils.isBlank(weekList.get(i).getId()) ) {//子表没有ID 插入
                    weekList.get(i).setParentId(pid);
                    weekList.get(i).setSortNo(i + "");
                    weekList.get(i).setWorkDateFrom(workDateFrom);
                    weekList.get(i).setWorkDateTo(workDateTo);
                    reportPlanMapper.insert(weekList.get(i));
                } else{
                    weekList.get(i).setSortNo(i + "");
                    reportPlanMapper.updateById(weekList.get(i));
                    ids2.add(weekList.get(i).getId());
                }
            }
        }
        //逻辑删除以前已存在但此次页面又删除的计划
        if(CollectionUtil.isNotEmpty(ids1)){
            for(String idStr:ids1){
                if(!ids2.contains(idStr)){
                    log.error("开始逻辑删除="+idStr);
                    reportPlanMapper.logicDeleteById(idStr);
                }
            }
        }
    }

    @Override
    public List<OaReportPlan> getReportWeekPlanById(String id) {
        return reportPlanMapper.getReportPlanById(id);
    }

    @Override
    public int deleteById(String id) {
        return reportPlanMapper.logicDeleteById(id);
    }

    @Override
    public List<OaReportPlan> getByParentIdAndPlanStatus0(String parentId) {
        return reportPlanMapper.getByParentIdAndPlanStatus0(parentId);
    }

    @Override
    public List<OaReportPlan> getByParentIdAndPlanStatus1(String parentId) {
        return reportPlanMapper.getByParentIdAndPlanStatus1(parentId);
    }

    @Override
    public List<OaReportPlan> getThisWeekPlan(String createId, Date lastWeekDate) {
        return reportPlanMapper.getThisWeekPlan(createId, lastWeekDate);
    }
}
